package iofile;

import java.io.*;

//演示二进制文件
//Buffered字节处理流
public class BufferedCopy02_ {

    public static void main(String[] args) {

        String srcFile = "";
        String destFile = "";
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            //BufferedInputStream接收一个inputStream对象，可以传入继承了inputStream的子类
            bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFile));

            //循环读取文件并写入
            byte[] buff = new byte[1024];
            int readLen = 0;
            //读取到文件末尾时，返回-1
            while ((readLen = bufferedInputStream.read(buff)) != -1){
                bufferedOutputStream.write(buff,0, readLen);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
